﻿<prism:VisualStateAwarePage 
    x:Name="pageRoot"
    x:Class="Sumc.Client.ModernUI.Views.StopPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sumc.Client.ModernUI.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:behaviors="using:Sumc.Client.ModernUI.Attachements"
    xmlns:converters="using:Sumc.Client.ModernUI.Converters"
    mc:Ignorable="d"
    prism:ViewModelLocator.AutoWireViewModel="True">

    <prism:VisualStateAwarePage.Resources>
        <DataTemplate x:Key="StopItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.LoadStop, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding Number}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/BusStop.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Text="{Binding Name}">
                    </TextBlock>
                    <TextBlock FontSize="14"
                               Margin="0"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap">
                        <Run x:Uid="StopPageRunNumber" />
                        <Run Text="{Binding Number}" />
                    </TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="BusItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.GoToSchedule, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding ScheduleUrl}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/Bus.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis">
                        <Run x:Uid="StopPageRunBus" />
                        <Run Text="{Binding Title}" />
                    </TextBlock>
                    <TextBlock Text="{Binding Route}"
                               FontSize="14"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap" />
                    <TextBlock Style="{StaticResource BodyTextStyle}"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               Text="{Binding ArrivalsTimes, Converter={StaticResource EnumerationToTextConverter}}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
        
        <DataTemplate x:Key="TramItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.GoToSchedule, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding ScheduleUrl}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/Tram.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis">
                        <Run x:Uid="StopPageRunTram" />
                        <Run Text="{Binding Title}" />
                    </TextBlock>
                    <TextBlock Text="{Binding Route}"
                               FontSize="14"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap" />
                    <TextBlock Style="{StaticResource BodyTextStyle}"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               Text="{Binding ArrivalsTimes, Converter={StaticResource EnumerationToTextConverter}}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="TrolleyItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.GoToSchedule, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding ScheduleUrl}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/Trolley.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis">
                        <Run x:Uid="StopPageRunTrolley" />
                        <Run Text="{Binding Title}" />
                    </TextBlock>
                    <TextBlock Text="{Binding Route}"
                               FontSize="14"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap" />
                    <TextBlock Style="{StaticResource BodyTextStyle}"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               Text="{Binding ArrivalsTimes, Converter={StaticResource EnumerationToTextConverter}}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </prism:VisualStateAwarePage.Resources>
    <prism:VisualStateAwarePage.TopAppBar>
        <AppBar>
            <Button Style="{StaticResource HomeAppBarButtonStyle}" 
                    Command="{Binding GoToHome}" />
        </AppBar>
    </prism:VisualStateAwarePage.TopAppBar>

    <Grid Style="{StaticResource LayoutRootStyle}"
          Background="{StaticResource Background}"
          x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="140" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <!--Not found template-->
        <Grid Margin="120,200,0,0"
              Grid.RowSpan="2"
              Visibility="{Binding IsFound, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}">
            <Grid Visibility="{Binding IsAuthenticated, Converter={StaticResource BooleanToVisibilityConverter}}">
                <TextBlock FontSize="18"
                           Foreground="{StaticResource DefaultWhiteBrush}"
                           x:Name="NotFoundTextBlock"
                           Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}"
                           x:Uid="StopPageTextBlockNotFound" />
            </Grid>

        </Grid>
        
        <ScrollViewer x:Name="MainScrollViewer"
                      Grid.RowSpan="2"
                      Padding="0,170,0,50"
                      HorizontalScrollBarVisibility="Auto"
                      HorizontalScrollMode="Enabled"
                      VerticalScrollMode="Disabled"
                      VerticalScrollBarVisibility="Disabled"
                      MaxZoomFactor="1">
            <StackPanel Orientation="Horizontal"
                        Margin="120,0,120,0">
                <Grid Margin="0,0,50,0"
                      Visibility="{Binding Buses, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock  FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockBuses" />
                    <GridView x:Name="BusesGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Buses, Mode=OneWay}"
                              ItemTemplate="{StaticResource BusItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>
                <Grid Margin="0,0,50,0"
                      Visibility="{Binding Trams, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock  FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockTrams" />
                    <GridView x:Name="TramsGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Trams, Mode=OneWay}"
                              ItemTemplate="{StaticResource TramItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>
                <Grid Margin="0,0,100,0"
                      Visibility="{Binding Trolleys, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockTrolleys" />
                    <GridView x:Name="TrolleysGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Trolleys, Mode=OneWay}"
                              ItemTemplate="{StaticResource TrolleyItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

                <Grid Visibility="{Binding FoundStops, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockAnotherStops" />
                    <GridView x:Name="StopsGridView"
                              Grid.Row="1"
                              Grid.Column="0"
                              ItemsSource="{Binding FoundStops, Mode=OneWay}"
                              ItemTemplate="{StaticResource StopItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

            </StackPanel>
        </ScrollViewer>
        <!--Snap-->
        <ScrollViewer x:Name="SnapMainScrollViewer"
                      Visibility="Collapsed"
                      Grid.RowSpan="2"
                      Padding="0,170,0,0"
                      HorizontalScrollBarVisibility="Disabled"
                      HorizontalScrollMode="Disabled"
                      VerticalScrollMode="Enabled"
                      VerticalScrollBarVisibility="Auto"
                      MaxZoomFactor="1">
            <StackPanel Orientation="Vertical"
                        Margin="5,0,0,0">
                <Grid Visibility="{Binding Buses, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockBuses" />
                    <GridView x:Name="SnapBusesGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Buses, Mode=OneWay}"
                              ItemTemplate="{StaticResource BusItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>
                <Grid Visibility="{Binding Trams, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockTrams" />
                    <GridView x:Name="SnapTramsGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Trams, Mode=OneWay}"
                              ItemTemplate="{StaticResource TramItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>
                <Grid Visibility="{Binding Trolleys, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock  FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="StopPageTextBlockTrolleys" />
                    <GridView x:Name="SnapTrolleysGridView"
                              Grid.Row="1"
                              Grid.Column="0" 
                              ItemsSource="{Binding Trolleys, Mode=OneWay}"
                              ItemTemplate="{StaticResource TrolleyItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

                <Grid Visibility="{Binding FoundStops, Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Style="{StaticResource PageSubheaderTextStyle}"
                               x:Uid="StopPageTextBlockAnotherStops" />
                    <GridView x:Name="SnapStopsGridView"
                              Grid.Row="1"
                              Grid.Column="0"
                              ItemsSource="{Binding FoundStops, Mode=OneWay}"
                              ItemTemplate="{StaticResource StopItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

            </StackPanel>
        </ScrollViewer>

        <!--Error Message-->
        <Grid Margin="120,0,0,100"
              VerticalAlignment="Bottom"
              Grid.RowSpan="2"
              x:Name="ErrorGrid"
              Visibility="{Binding IsAuthenticated, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <Button Style="{StaticResource RefreshAppBarButtonStyle}"
                    Command="{Binding ShowFlyout}" 
                    Padding="0"
                    Margin="-30,0,0,0"/>
            <TextBlock FontSize="22"
                       Grid.Column="2"
                       TextAlignment="Left"
                       Margin="0,19,0,0"
                       Foreground="{StaticResource DefaultWhiteBrush}"
                       x:Name="ErrorTextBlock"
                       TextWrapping="Wrap"
                       TextTrimming="WordEllipsis"
                       Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}"
                       x:Uid="SearchPageTextBlockOpenFlyout" />
        </Grid>
        
        <!-- Back button and page title -->
        <Grid Visibility="{Binding Path=Stop.Name, Converter={StaticResource NullToVisibilityConverter}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton"
                    Command="{Binding GoBack}"
                    Style="{StaticResource BackButtonStyle}" />
            <Image x:Name="AppLogoImage"
                   Grid.Column="1" Source="ms-appx:///Assets/MainLogo.png"
                   Stretch="None"
                   HorizontalAlignment="Left" NineGrid="0" VerticalAlignment="Top" Margin="0,50,0,0"/>
            <TextBlock x:Name="PageTitle"
                       Grid.Column="3"
                       Style="{StaticResource PageHeaderTextStyle}" HorizontalAlignment="Left" Margin="20,0,30,40"
                       DataContext="{Binding Stop}">
                <Run Text="{Binding Name}" />
            </TextBlock>
        </Grid>

        <ProgressBar IsIndeterminate="{Binding LoadingData}"
                     Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}}"
                     Style="{StaticResource PageProgressBar}"
                     Grid.Row="1" />

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape" />
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource PortraitBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ErrorTextBlock"
                                                       Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="250" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ErrorGrid"
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="30,0,0,100" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainScrollViewer"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SnapMainScrollViewer"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Visible" />
                        </ObjectAnimationUsingKeyFrames>
                        <!-- Header -->
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitle"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedPageHeaderTextStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="AppLogoImage"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</prism:VisualStateAwarePage>
